package com.ruoyi.comparison.utils;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Configuration
public class PDFUtil {
    public static Map<String,Integer> PDFByKeyWord(String newFileMix, Set<String> set){
        Map<String,Integer> map=new HashMap<>();
        for (String key : set) {
            try {
                InputStream inputStream = Files.newInputStream(Paths.get(newFileMix));
                PdfReader reader = new PdfReader(inputStream);
                int n = reader.getNumberOfPages();
                for (int i = 1; i<= n; i++) {
                    String textFromPage = PdfTextExtractor.getTextFromPage(reader, i);
                    String s = textFromPage.replaceAll("\\s*", "");
                    if (s.length()<6){
                        continue;
                    }
                    if (s.contains(key)){
                        map.put(key,i);
                        break;
                    }
                }
                inputStream.close();
                reader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return map;
    }

//    public static List<String> parsePDF(String bidFile, String leftFile, String rightFile){
//
//        return null;
//    }
    public static String getText(String file){
        String s = null;
        StringBuilder builder = new StringBuilder();
        List<String> list = new ArrayList<>();
        Set<String> set1 = new HashSet<>();
        Set<String> set2 = new HashSet<>();
        Set<String> set3 = new HashSet<>();
        Set<String> set5 = new HashSet<>();
        Set<String> set6 = new HashSet<>();
        Set<String> set7 = new HashSet<>();

        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        List<String> list3 = new ArrayList<>();
        List<String> list5 = new ArrayList<>();
        List<String> list6 = new ArrayList<>();
        List<String> list7 = new ArrayList<>();
        try {
            InputStream inputStream = Files.newInputStream(Paths.get(file));
            PdfReader reader = new PdfReader(inputStream);
            int n = reader.getNumberOfPages();
            for (int i = 1; i<= n; i++) {
                String textFromPage = PdfTextExtractor.getTextFromPage(reader, i);
                String[] split = textFromPage.split("\n");
                int length = split.length;
                if (length >1){
                    set1.add(split[0]);
                    list1.add(split[0]);
                }
                if (length >2){
                    set2.add(split[1]);
                    list2.add(split[1]);
                    set5.add(split[length -1]);
                    list5.add(split[length -1]);
                }
                if (length >3){
                    set3.add(split[2]);
                    list3.add(split[2]);
                    set6.add(split[length -2]);
                    list6.add(split[length -2]);
                }
                if (length >4){
                    set7.add(split[length -3]);
                    list7.add(split[length -3]);
                }
                list.add(textFromPage);
            }
            inputStream.close();
            reader.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        int size = list.size();
        for (int i = 0; i < size; i++) {
            String s1 = list.get(i);
            String[] split = s1.split("\n");
            String substring = null;
            int i1 = split[0].length()+1;
            if (set1.size()< list1.size()-2){
                substring = s1.substring(i1);
            }
            int i2 = split[1].length();
            if (set2.size()< list2.size()-2){
                substring = s1.substring(i2+1);
            }
            int i3 = split[2].length();
            if (set3.size()< list3.size()-2){
                substring = s1.substring(1+i3);
            }
            int i4 = split.length - 1;
            if (set5.size()< list5.size()-2 ||StringUtils.isNumeric(split[i4])){
                substring = s1.substring(0,s1.length()-split[i4].length()-2);
            }
            int i5 = split.length - 2;
            if (set6.size()< list6.size()-2 ||StringUtils.isNumeric(split[i5])){
                substring = s1.substring(0,s1.length()-split[i5].length()-2);
            }
            int i6 = split.length - 3;
            int i7 = split.length - 4;
            if (set7.size()< list7.size()-2 ||StringUtils.isNumeric(split[i6])){
                substring = s1.substring(0,s1.length()-split[i6].length()-2);
            }else if (StringUtils.isNumeric(split[i7])) {
                substring = s1.substring(0, s1.length() - split[i7].length() - 2);
            }
            builder.append(substring);
        }
        return builder.toString();
    }

}
